package com.zt.english.activity
import android.widget.TextView
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.zt.english.databinding.ActivityTranslateBinding
import com.zt.english.utils.TranslateUtil
import com.zt.mvvm.viewmodel.BaseViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class TranslateViewModel :BaseViewModel<TranslateRepository,ActivityTranslateBinding>(){
    override fun initView() {

    }
    override fun initData() {
//        translateText("history",mBinding.txt)
//        translateText("fly me to the moon",mBinding.txt)
//        translateText("中国",mBinding.txt)
    }
    fun translateText(q:String,txt:TextView){
        val map = TranslateUtil.parameters(q)
        viewModelScope.launch {
            mRepository.translateTxt(map)
                .collectLatest {
                    val json = Gson().toJson(it)
                    txt.text = json
                }
        }
    }
}